from pygame.sprite import Sprite
import pygame
from point import Point
from arkanoid_errors import UserDisq
class Ball (Sprite):
    mouse_clicked = False
    point = Point()
    
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image = pygame.image.load('ball.png').convert()
        self.image.set_colorkey(self.image.get_at((0,0)))
        self.rect = self.image.get_rect()
        self.rect = self.rect.move((25,370));
    
    def update(self):
        pass
    
    def move(self, event):
        if self.mouse_clicked == False and event != None:
            x,y = event.pos
            if ( x < 800 - self.rect.width - 60 ):
                self.rect = pygame.Rect(x + 25, 370, self.rect.width, self.rect.height)
        if self.mouse_clicked and event == None:
            self.handle_collisions()
            self.point.move()
            self.rect = pygame.Rect(self.point.x, self.point.y, self.rect.width, self.rect.height)
            
    def handle_collisions(self):
        if self.point.y >= 400-self.rect.height:
            raise UserDisq()
        if self.point.x >= ( 800 - self.rect.width )  or self.point.x <= 0:
            self.point.collide_x()
        if self.point.y >= ( 400 - self.rect.height ) or self.point.y <= 0:
            self.point.collide_y( None )
            
    def collide_item( self, bar_velocity ):
        self.point.collide_y( bar_velocity )
        
        
    def handle_mouse_click(self, clicked):
        self.mouse_clicked = clicked
        if ( clicked ):
            self.point.x = self.rect.x
            self.point.y = self.rect.y
        else:
            self.point = Point()
        
        